<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../../assets/login.jpg" alt />
      </div>
      <el-form :model="loginForm" ref="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model ="loginForm.username" clearable autocomplete="off" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="loginForm.password" autocomplete="off" placeholder="请输入密码" type="password" show-password></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          <el-button type="info" @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    // 自定义校验
    var checkName = (rule, value, callback) => {
      setTimeout(() => {
        if (value !== '' && value !== undefined && value === 'george') {
          return callback(new Error('用户名已存在'))
        } else {
          callback()
        }
      }, 1000)
    }
    return {
      // 登陆表单数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单校验
      loginFormRules: {
        // 校验用户名是否合法
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
          { pattern: /^\S+$/, message: '不能含有空格' },
          { validator: checkName, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 表单提交，在提交时进行一次校验
    submitForm (formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) {
          this.$message.error('登录失败')
          return false
        } else {
          const { data: result } = await this.$http.post('/ioms-system/api/system/login', this.loginForm)
          console.log(result)
          if (result.code !== 200) {
            this.$message.error('登录失败！')
            return
          }
          this.$message.success('登陆成功！')
          // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
          // 1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
          // 1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
          window.sessionStorage.setItem('Authorization', result.data)

          // 2. 通过编程式导航跳转到后台主页，路由地址是 /home
          this.$router.push('/home')
        }
      })
    },
    // 表单重置
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b5b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btn {
  display: flex;
  // flex布局，按钮置于右下角
  justify-content: flex-end;
  // 按钮居中
  // justify-content: center;
}
</style>
